package com.zh.api.controller.operation;

import com.alibaba.fastjson.JSONObject;
import com.zh.common.annotation.Log;
import com.zh.common.base.BaseController;
import com.zh.common.utils.*;
import com.zh.familyInfo.domain.FamilyFamilyUserDO;
import com.zh.familyInfo.domain.FamilyUserDTO;
import com.zh.familyInfo.service.FamilyFamilyUserService;
import com.zh.operation.domain.BedRestDO;
import com.zh.operation.service.BedRestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@Api("午休【床位午休信息】")
public class BedRestController extends BaseController {
	@Autowired
	private BedRestService bedRestService;
	@Autowired
	private FamilyFamilyUserService familyUserService;

	@Log("今日所有床位老人午休信息")
	@ResponseBody
	@RequestMapping(value = "listOperationBedRest", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO listOperationBedRest(@RequestBody Map<String, Object> params){
		if(CheckEmptyUtil.isEmpty(params)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		//查询列表数据
//        Query query = new Query(params);
		List<BedRestDO> bedRestList = bedRestService.list(params);
		//查询老人姓名
		for (BedRestDO nu:bedRestList){
			FamilyFamilyUserDO familyUserDO = familyUserService.get(nu.getUserId());
			if (familyUserDO !=null && familyUserDO.getName()!=null) {
				nu.setName(familyUserDO.getName());
				if(familyUserDO.getSex()!=null){
					nu.setSex(familyUserDO.getSex());
				}
			}else{
				continue;
			}
		}
		int total = bedRestService.count(params);
		PageUtils pageUtils = new PageUtils(bedRestList, total);
		return ResultVO.success(pageUtils);
	}


	@Log("编辑床位老人午休信息")
	@ResponseBody
	@RequestMapping(value = "editOperationBedRestById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO editOperationBedRestById(@RequestBody BedRestDO bedRest){
		if(bedRest==null || CheckEmptyUtil.isEmpty(bedRest.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		BedRestDO bedRestDO = bedRestService.get(bedRest.getId());
	    return ResultVO.success(bedRestDO);
	}

	@Log("新增今日床位午休信息")
	@ResponseBody
	@RequestMapping(value = "saveOperationBedRest", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO saveOperationBedRest(@RequestBody BedRestDO bedRest){
		if(bedRest==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		bedRest.setCreateBy(getUserId());
		if(bedRestService.save(bedRest)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}

	@Log("更新今日床位午休信息")
	@ResponseBody
	@RequestMapping(value = "updateOperationBedRestById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO updateOperationBedRestById(@RequestBody  BedRestDO bedRest){
		if(bedRest==null || CheckEmptyUtil.isEmpty(bedRest.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		bedRestService.update(bedRest);
		return ResultVO.success();
	}


	@Log("删除午休信息")
	@ResponseBody
	@RequestMapping(value = "removeOperationBedRestById", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ResultVO removeOperationBedRestById(@RequestBody  BedRestDO bedRest){
		if(bedRest==null || CheckEmptyUtil.isEmpty(bedRest.getId())){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		if(bedRestService.remove(bedRest.getId())>0){
		return ResultVO.success();
		}
		return ResultVO.error();
	}
	@RequestMapping(value = "batchRemoveOperationBedRestByIds", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	@ApiOperation("批量删除今日床位午休信息")
	public ResultVO batchRemove(@RequestBody JSONObject jsonParam){
		if(jsonParam==null){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		List<String> ids= (List<String>)jsonParam.get("ids");
		if(CheckEmptyUtil.isEmpty(ids)){
			return ResultVO.error(ResultEnum.PARAME_ERROR);
		}
		String[] strs1=ids.toArray(new String[ids.size()]);
		if(bedRestService.batchRemove(strs1)>0){
			return ResultVO.success();
		}
		return ResultVO.error();
	}

}
